package com.xomily.devops.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xomily.common.utils.DateUtils;
import com.xomily.common.utils.sign.Md5Utils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xomily.devops.mapper.DevAlarmMapper;
import com.xomily.devops.mapper.DevCpuLogMapper;
import com.xomily.devops.mapper.DevHostMapper;
import com.xomily.devops.domain.DevAlarmLog;
import com.xomily.devops.domain.DevCpuLog;
import com.xomily.devops.domain.DevHost;
import com.xomily.devops.service.IDevAlarmLogService;
import com.xomily.devops.service.IDevCpuLogService;
import com.xomily.system.service.ISysConfigService;
/**
 * CPU监测日志Service业务层处理
 * 
 * @author xomily
 * @date 2021-11-29
 */
@Service
public class DevCpuLogServiceImpl implements IDevCpuLogService 
{
    @Autowired
    private DevCpuLogMapper devCpuLogMapper;
    @Autowired
    private DevHostMapper devHostMapper;
    @Autowired
    private DevAlarmMapper devAlarmMapper;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IDevAlarmLogService  alarmLogService;    
    
    /**
     * 查询CPU监测日志
     * 
     * @param id CPU监测日志主键
     * @return CPU监测日志
     */
    @Override
    public DevCpuLog selectDevCpuLogById(Long id)
    {
        return devCpuLogMapper.selectDevCpuLogById(id);
    }

    /**
     * 查询CPU监测日志列表
     * 
     * @param devCpuLog CPU监测日志
     * @return CPU监测日志
     */
    @Override
    public List<DevCpuLog> selectDevCpuLogList(DevCpuLog devCpuLog)
    {
        return devCpuLogMapper.selectDevCpuLogList(devCpuLog);
    }

    /**
     * 查询CPU监测日志列表
     * 
     * @param Map 条件
     * @return 集合
     */

    public Map<String, Object> selectDevCpuLogByGraph(Map<String, Object> params) {
		double[] data = new double[240];
		String[] xAxis = new String[240];
		List<DevCpuLog> logList = devCpuLogMapper.selectDevCpuLogByGraph(params);
		int h,m,n;
		
		for(int i=0; i<240; i++){
			h = (i/10);
			m = (i%10*6);
			xAxis[i]= String.format("%02d",h)+":"+String.format("%02d",m); 
		} 
		
		for (DevCpuLog log : logList) {
            Date createTime = log.getCreateTime();
            h = DateUtils.getHour(createTime);
            m = DateUtils.getMinute(createTime); 
            n = (h*60 + m)/6;			
			if (log.getUseRate()>data[n]) {
				data[n]= log.getUseRate();
			}
		}		
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("cpuGraph",data);
        ret.put("xAxis",xAxis);
		return ret;
	}

    
    /**
     * 查询温度监测日志列表
     * 
     * @param Map 条件
     * @return 集合
     */

    public Map<String, Object> selectDevTemperatureLogByGraph(Map<String, Object> params) {
		double[] data = new double[240];
		String[] xAxis = new String[240];
		List<DevCpuLog> logList = devCpuLogMapper.selectDevCpuLogByGraph(params);
		int h,m,n;
		
		for(int i=0; i<240; i++){
			h = (i/10);
			m = (i%10*6);
			xAxis[i]= String.format("%02d",h)+":"+String.format("%02d",m); 
		} 
		
		for (DevCpuLog log : logList) {
            Date createTime = log.getCreateTime();
            h = DateUtils.getHour(createTime);
            m = DateUtils.getMinute(createTime); 
            n = (h*60 + m)/6;			
			if (log.getTemperature()>data[n]) {
				data[n]= log.getTemperature();
			}
		}		
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("temperatureGraph",data);
        ret.put("xAxis",xAxis);
		return ret;
	}

    
    /**
     * 新增CPU监测日志
     * 
     * @param devCpuLog CPU监测日志
     * @return 结果
     */
    @Override
    public int insertDevCpuLog(DevCpuLog devCpuLog)
    {

        DevHost hostQuery = devHostMapper.selectDevHostByIp(devCpuLog.getIp());
        if (hostQuery != null ) {
         	DevHost host = new DevHost();
        	host.setId(hostQuery.getId());
            host.setTemperature(devCpuLog.getTemperature());
            host.setFan(devCpuLog.getFan());       	
            host.setCpuRate(devCpuLog.getUseRate());
        	host.setUpdateTime(DateUtils.getNowDate());
        	int devAlarmCount=devAlarmMapper.selectDevAlarmByIp(devCpuLog.getIp());
        	if (devAlarmCount == 0) {
            	host.setRunStatus("1");        		
        	}else {
            	host.setRunStatus("2");	
        	}
        	
        	devHostMapper.updateDevHostCpu(host);   
        }
        

	    double cpuUseRate = Double.parseDouble(configService.selectConfigByKey("dev.host.cpuUseRate"));
        if ((cpuUseRate>0) && (devCpuLog.getUseRate()>cpuUseRate)) {
        	DevAlarmLog alarmLog = new DevAlarmLog();
            String content = "CPU使用率大于告警值"+ configService.selectConfigByKey("dev.host.cpuUseRate")+"%";
        	alarmLog.setType("CpuWarn");
            alarmLog.setIp(devCpuLog.getIp());
            alarmLog.setCreateTime(DateUtils.getNowDate());
            alarmLog.setGenerateTime(devCpuLog.getGenerateTime());
            alarmLog.setContent(content);
            alarmLog.setCode( Md5Utils.hash(alarmLog.getType()+alarmLog.getContent()));
            alarmLogService.insertDevAlarmLog(alarmLog);
        }
        
	    double temperature = Double.parseDouble(configService.selectConfigByKey("dev.host.temperature"));
        if ((temperature>0) && (devCpuLog.getTemperature()>temperature)) {
        	DevAlarmLog alarmTemperatureLog = new DevAlarmLog();
            String content = "CPU温度大于告警值"+ configService.selectConfigByKey("dev.host.temperature")+"℃";
            alarmTemperatureLog.setType("TemperatureWarn");
            alarmTemperatureLog.setIp(devCpuLog.getIp());
            alarmTemperatureLog.setCreateTime(DateUtils.getNowDate());
            alarmTemperatureLog.setGenerateTime(devCpuLog.getGenerateTime());
            alarmTemperatureLog.setContent(content);
            alarmTemperatureLog.setCode( Md5Utils.hash(alarmTemperatureLog.getType()+alarmTemperatureLog.getContent()));
            alarmLogService.insertDevAlarmLog(alarmTemperatureLog);
        }

        devCpuLog.setCreateTime(DateUtils.getNowDate());
        devCpuLog.setHostId(hostQuery.getId());
        return devCpuLogMapper.insertDevCpuLog(devCpuLog);
    }

    /**
     * 修改CPU监测日志
     * 
     * @param devCpuLog CPU监测日志
     * @return 结果
     */
    @Override
    public int updateDevCpuLog(DevCpuLog devCpuLog)
    {
        return devCpuLogMapper.updateDevCpuLog(devCpuLog);
    }

    /**
     * 批量删除CPU监测日志
     * 
     * @param ids 需要删除的CPU监测日志主键
     * @return 结果
     */
    @Override
    public int deleteDevCpuLogByIds(Long[] ids)
    {
        return devCpuLogMapper.deleteDevCpuLogByIds(ids);
    }

    /**
     * 删除CPU监测日志信息
     * 
     * @param id CPU监测日志主键
     * @return 结果
     */
    @Override
    public int deleteDevCpuLogById(Long id)
    {
        return devCpuLogMapper.deleteDevCpuLogById(id);
    }
}
